/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.controller.MainController;
import com.bayu.services.dao.StockDao;
import com.bayu.services.entity.Stock;
import java.awt.Color;
import java.io.OutputStream;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author submonkey
 */
@Controller
public class StockBarGraph {

    private String query;
    private StockDao stockDao;
//    private double max;

    @Autowired
    public void setSSDao(StockDao stockDao) {
        this.stockDao = stockDao;
    }

    @RequestMapping("/stock-bargraph.png")
    public void drawChart(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart, 1480, 800);
    }

    private JFreeChart getChart() {
        DefaultCategoryDataset dataset = getDataset();

        final JFreeChart chart = ChartFactory.createBarChart(
            "Stock and Cummulative Incoming/ Usage",
            "Date",
            "M/T",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        chart.setBackgroundPaint(new Color(204, 255, 204));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.green);
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer.setBaseItemLabelsVisible(true);

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
//        rangeAxis.setRange(0, max);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        return chart;
    }

    private DefaultCategoryDataset getDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        List data = this.stockDao.getAllByDate(MainController.startDate, MainController.endDate);
        for (Object object : data) {
            Stock stock = (Stock) object;
            String date[] = stock.getDate().toString().split("-");
//            double planIncoming = stock.getPlanIncoming();
//            double incoming = stock.getIncoming();
//            double planUsage = stock.getPlanUsage();
//            double usage = stock.getUsage();
//            double value = (planIncoming > incoming ? (planIncoming > planUsage ? (planIncoming > usage ? planIncoming : usage) : (planUsage > usage ? planUsage : usage)) : (incoming > planUsage ? (incoming > usage ? incoming : usage) : (planUsage > usage ? planUsage : usage)));
//            max = value > max ? value : max;
            dataset.addValue(stock.getPlanincoming(), "Plan Incoming", date[2]);
            dataset.addValue(stock.getIncoming(), "Incoming", date[2]);
            dataset.addValue(stock.getPlanusage(), "Plan Usage", date[2]);
            dataset.addValue(stock.getUsagedata(), "Usage", date[2]);
        }

        return dataset;
    }
}

